<template>
	<uv-popup ref="popupBox" mode="bottom" :round="10" @maskClick="closeFunc">
		<view class="title">提交预付款凭证</view>
		<scroll-view class="scroll-view" scroll-y>
			<view class="uv-form-box">
				<uv-form labelPosition="top" :model="formData" :rules="rules" ref="form" labelWidth="auto">
					<uv-form-item label="预付款金额" prop="amount_collected" required>
						<uv-input type="digit" placeholder="请输入预付款金额" v-model="formData.amount_collected"></uv-input>
					</uv-form-item>
					<uv-form-item label="收款账户" prop="receiving_account_id" @click="selectReceivingAccount" required>
						<uv-input readonly placeholder="请选择" v-model="formData.receiving_account_id"></uv-input>
					</uv-form-item>
					<uv-form-item label="预付款凭证 (.jpg,.png,.gif)" prop="payment_voucher" labelPosition="left" required>
						<view class="file-box">
							<uv-button
								type="primary"
								text="点击上传"
								@click="uploadFunc"
								customTextStyle="font-weight: 400;font-size: 24rpx;color: #FFFFFF;"
								customStyle="width: 170rpx;height: 60rpx;background: #3B75FF;border-radius: 16rpx;"
							></uv-button>
						</view>
					</uv-form-item>
					<view class="file-list">
						<view class="file-media" v-for="(item, index) in formData.payment_voucher" :key="index">
							<view class="del-icon" @click.stop="delFile(index, 'payment_voucher')">
								<uv-icon name="trash-fill" size="18" color="#ffffff"></uv-icon>
							</view>
							<image mode="aspectFill" :src="item.tempFilePath" class="media-item"></image>
						</view>
					</view>
					<uv-form-item label="备注" prop="remark">
						<uv-textarea height="100" v-model="formData.remark" placeholder="请输入内容"></uv-textarea>
					</uv-form-item>
				</uv-form>
			</view>
		</scroll-view>
		<view class="btn-box">
			<uv-button
				@click="closeFunc"
				text="取 消"
				customTextStyle="font-weight: 400;font-size: 28rpx;color: #8F8F8F;"
				customStyle="width: 180rpx;height: 80rpx;background: #FFFFFF;border-radius: 16rpx;border: 2rpx solid #E3E3E3;margin-bottom:20rpx;"
			></uv-button>
			<uv-button
				@click="formSubmit"
				type="primary"
				text="提 交"
				customTextStyle="font-weight: 400;font-size: 28rpx;color: #FFFFFF;"
				customStyle="width: 500rpx;height: 80rpx;background: #3B75FF;border-radius: 16rpx;margin-bottom:20rpx;"
			></uv-button>
		</view>
	</uv-popup>
	<!-- 账户列表 -->
	<uv-picker title="账户列表" ref="zhPicker" :columns="zhPolumns" @confirm="zhConfirm" keyName="account_name"></uv-picker>
</template>

<script setup>
import { computed, watch, onMounted, getCurrentInstance, ref, defineEmits } from 'vue';
const { proxy: that } = getCurrentInstance();
let props = defineProps({
	show: {
		type: Boolean,
		default: () => {
			return false;
		}
	},
	dataObj: {
		type: Object,
		default: () => {
			return {};
		}
	},
	dkData: {
		default: () => {
			return {};
		}
	},
	zjData: {
		default: () => {
			return {};
		}
	},
	signing_company_data: {
		default: () => {
			return {};
		}
	}
});
// 账户列表
let zhPolumns = ref([]);
// 表单数据
let formData = ref({
	amount_collected: '',
	receiving_account_id: '',
	receiving_account_data: '',
	payment_voucher: [],
	remark: ''
});
// 表单规则
let rules = ref({
	amount_collected: {
		required: true,
		message: '请输入预付款金额',
		trigger: ['blur', 'change']
	},
	receiving_account_id: {
		required: true,
		message: '请选择收款公司',
		trigger: ['blur', 'change']
	},
	payment_voucher: [
		{
			asyncValidator: (rule, value, callback) => {
				if (!value.length) {
					callback(new Error('请上传预付款凭证'));
				} else {
					// 如果校验通过，也要执行callback()回调
					callback();
				}
			}
			// 如果是异步校验，无需写message属性，错误的信息通过Error抛出即可
			// message: 'xxx'
		}
	]
});

onMounted(() => {
	// that.$refs.popupBox.open();
	// getCompanyList();
});

watch(
	() => props.show,
	(newValue, oldValue) => {
		console.log(newValue, oldValue, '值变化');
		if (props.show) {
			// signing_company_data
			formData.value.receiving_account_id = props.signing_company_data.signing_company_name;
			formData.value.receiving_account_data = {
				account_name: props.signing_company_data.signing_company_name,
				id: props.signing_company_data.signing_company_id
			};
			getCompanyList();
			that.$refs.popupBox.open();
		} else {
			formData.value = {
				amount_collected: '',
				receiving_account_id: '',
				receiving_account_data: '',
				payment_voucher: [],
				remark: ''
			};
			that.$refs.form.clearValidate();
			that.$refs.popupBox.close();
		}
	}
);
const emit = defineEmits(['close', 'onSuccess']);

const closeFunc = () => {
	emit('close', false);
};
// 上传文件
const uploadFunc = () => {
	uni.chooseMedia({
		mediaType: ['image'], //, 'video'
		sourceType: ['album', 'camera'],
		count: 1,
		success(res) {
			console.log(res.tempFiles);
			res.tempFiles.map((item) => {
				formData.value.payment_voucher = [item];
				that.$refs.form.validateField('payment_voucher');
			});
		}
	});
};
// 删除文件
const delFile = (index) => {
	formData.value.payment_voucher.splice(index, 1);
	that.$refs.form.validateField('payment_voucher');
};

// 账户列表
const getCompanyList = () => {
	zhPolumns.value = [];
	that.$Api
		.getCompanyList({
			page: 1,
			num: 9999,
			account_type: 1
		})
		.then((res) => {
			console.log(res, '账户列表');
			if (res.code == 200) {
				zhPolumns.value.push(res.data.list);
			}
		});
};
// 选择账户
const zhConfirm = (e) => {
	console.log(e);
	formData.value.receiving_account_id = e.value[0].account_name;
	formData.value.receiving_account_data = e.value[0];
	that.$refs.form.validateField('receiving_account_id');
};
const selectReceivingAccount = () => {
	that.$refs.zhPicker.open();
};

// 表单提交
const formSubmit = () => {
	let data = formData.value;
	let dataObj = props.dataObj;
	console.log(data, '表单数据', dataObj);
	that.$refs.form
		.validate()
		.then((res) => {
			uni.showLoading({
				mask: true,
				title: '文件上传中...'
			});
			that.$upload(data.payment_voucher).then((res) => {
				console.log(1111, res);
				uni.hideLoading();

				uni.showLoading({
					mask: true,
					title: '信息提交中...'
				});
				that.$Api
					.wxSubmitAdvancePayment({
						payment_voucher: res.arrResult,
						project_id: dataObj.id,
						receiving_account_id: data.receiving_account_data.id,
						receiving_account_name: data.receiving_account_id,
						remark: data.remark,
						amount_collected: data.amount_collected
					})
					.then((res) => {
						uni.hideLoading();
						if (res.code == 200) {
							emit('onSuccess', true);
						}
						uni.showToast({
							title: res.code == 200 ? '操作成功' : res.message,
							icon: res.code == 200 ? 'success' : 'error'
						});
					})
					.catch((err) => {
						uni.hideLoading();
					});
			});
		})
		.catch((errors) => {});
};
// const statusText = computed(() => {

// });
</script>

<style lang="scss" scoped>
.title {
	font-size: 32rpx;
	width: 100%;
	line-height: 100rpx;
	text-align: center;
}
.btn-box {
	width: 100%;
	display: flex;
	align-items: center;
	justify-content: space-between;
	padding: 0 30rpx;
	box-sizing: border-box;
	margin-top: 30rpx;
}
.scroll-view {
	width: 100%;
	max-height: 50vh;
	background-color: #fff;
	// padding: 0 30rpx;
	// box-sizing: border-box;
	.uv-form-box {
		padding: 0 30rpx !important;
	}
	.file-box {
		width: 100%;
		display: flex;
		flex-direction: row-reverse;
	}
	.file-list {
		width: 100%;
		margin-bottom: 20rpx;
		display: flex;
		align-items: center;
		flex-wrap: wrap;
		.file-item {
			display: flex;
			justify-content: space-between;
			margin-top: 10rpx;
			margin-left: 10rpx;
			padding: 10rpx;
			background-color: #f8f8f8;
			width: 90%;

			.file-name {
				font-size: 24rpx;
				overflow: hidden; /* 确保溢出文本会被隐藏 */
				white-space: nowrap; /* 确保文本在一行内显示 */
				text-overflow: ellipsis; /* 使用省略号显示文本溢出的部分 */
				width: 80%;
			}
		}
		.file-media {
			width: 150rpx;
			height: 150rpx;
			margin: 0 27rpx 22rpx 0;
			overflow: hidden;
			border-radius: 10rpx;
			position: relative;
			&:nth-child(4n) {
				margin-right: 0;
			}
			.media-item {
				width: 100%;
				height: 100%;
			}
			.icon-box {
				position: absolute;
				z-index: 1;
				top: 50%;
				left: 50%;
				transform: translate(-50%, -50%);
				background-color: rgba(0, 0, 0, 0.6);
				width: 70rpx;
				height: 70rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				border-radius: 50%;
			}
			.del-icon {
				width: 60rpx;
				height: 40rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				position: absolute;
				right: 10rpx;
				top: 10rpx;
				z-index: 1;
				background-color: rgba(0, 0, 0, 0.6);
				border-radius: 10rpx;
			}
		}
	}
}
</style>
